package com.kaka.bus.engine.borrow.phone;

import android.content.Intent;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.home.main.limit.LimitActivity;
import com.kaka.bus.engine.other.DrainageActivity;
import com.kaka.bus.util.U;
import com.kaka.bus.view.dialog.GifDialog;
import com.kaka.bus.view.dialog.MessageDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import rx.Subscription;

/**
 * Created by zhangqi on 2016/10/18.
 * e-mail : ${email}
 * desc :
 */

public class PhoneYzmActivity extends BaseActivity<PhoneYzmPresenter> implements PhoneYzmContract.View {

    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_phoneyzm_tips)
    TextView mTvPhoneyzmTips;
    @BindView(R.id.edt_phoneyzm_service)
    EditText mEdtPhoneyzmService;
    @BindView(R.id.edt_phoneyzm_yzm)
    EditText mEdtPhoneyzmYzm;
    @BindView(R.id.tv_phoneyzm_send)
    TextView mTvPhoneyzmSend;
    @BindView(R.id.rl_phoneyzm)
    RelativeLayout mRlPhoneyzm;
    @BindView(R.id.btn_phoneyzm_commit)
    AppCompatButton mBtnPhoneyzmCommit;

    private String mPhone;
    private String mID;
    private String mType;
    private boolean mIsKefu;
    private String mYunying;
    private GifDialog mGifDialog;
    private MessageDialog mDialog;
    private String mLingshi;
    private Subscription mSubscribe;

    @Override
    protected void initView() {
        super.initView();
        mToolbarName.setText("手机认证");
        mToolbarBack.setOnClickListener(v -> finish());
        mGifDialog = new GifDialog(this);
        mDialog = new MessageDialog(this);

        Intent intent = getIntent();
        if (intent == null) {
            return;
        }
        mPhone = intent.getStringExtra(PhoneActivity.PHONE);
        mID = intent.getStringExtra(PhoneActivity.ID);
        mType = intent.getStringExtra("type");
        send();

        if ("1".equals(mType)) {
            mIsKefu = false;
            mEdtPhoneyzmService.setVisibility(View.GONE);
            mRlPhoneyzm.setVisibility(View.VISIBLE);
        } else if ("2".equals(mType)) {
            mIsKefu = true;
            mRlPhoneyzm.setVisibility(View.GONE);
            mEdtPhoneyzmService.setVisibility(View.VISIBLE);
        }

        String tips = getString(R.string.phoneyzm_tips1);
        mTvPhoneyzmTips.setText(tips);
    }

    @Override
    protected void initListener() {
        RxView.clicks(mTvPhoneyzmSend)
                .subscribe(v -> {
                    sendYzm();
                });


        mBtnPhoneyzmCommit.setOnClickListener(v -> {
            if (mIsKefu) {
                String service = mEdtPhoneyzmService.getText().toString();
                if(!TextUtils.isEmpty(service)){
                    commitYzm();
                }else{
                    showMessage("请填写验证码");
                }
            }else{
                String service = mEdtPhoneyzmYzm.getText().toString();
                if(!TextUtils.isEmpty(service)){
                    commitYzm();
                }else{
                    showMessage("请填写验证码");
                }
            }
        });
    }

    private void commitYzm() {
        Map<String,String> map = new HashMap<>();
        map.put(Constant.USERID,getUserId());
        if (mIsKefu) {
            map.put("command_code","10022");
            String yzm = mEdtPhoneyzmService.getText().toString();
            /**
             * 客服密码
             */
            map.put("query_code",yzm);
            map.put("token",getToken());
            mPresenter.commitYzm(map,"核对验证码中..");
        } else {
            map.put("command_code",mID);
            String yzm = mEdtPhoneyzmYzm.getText().toString();
            /**
             * 客服密码
             */
            map.put("verify_code",yzm);
            map.put("token",getToken());
            mPresenter.commitYzm(map,"核对验证码中..");
        }
    }


    /**
     * 发送验证码
     *
     * @return
     */
    private void sendYzm() {
        Map<String,String> map = new HashMap<>();
        map.put(Constant.USERID,getUserId());
        map.put("command_code","10004");
        map.put("token",getToken());
        mPresenter.commitYzm(map, "发送验证码");
    }

    @Override
    public void time() {
        send();
    }

    @Override
    public void saveId(String id) {
        mID = id;
    }

    private void send() {
        mTvPhoneyzmSend.setEnabled(false);
        if(mSubscribe != null && !mSubscribe.isUnsubscribed()){
            mSubscribe.unsubscribe();
        }
        //倒计时60秒
        //每秒赋值
        //提示错误信息
        //倒计时60秒
        //每秒赋值
        //提示错误信息
        mSubscribe = U.countdown(59)            //倒计时60秒
                .subscribe(
                        time -> mTvPhoneyzmSend.setText("重新发送" + "(" + time + ")"),    //每秒赋值
                        U::errorUtil,             //提示错误信息
                        () -> {
                            mTvPhoneyzmSend.setEnabled(true);
                            mTvPhoneyzmSend.setText(getString(R.string.reSendyzm));
                        });
    }


    @Override
    public void timeOut(HttpWrapper<String> wrapper) {
        U.tokenOut(this);
    }

    @Override
    public void getResult(HttpWrapper<String> wrapper) {
        switch (wrapper.getCode()){
            case "8888":
            case "10008":
            case "0000":
                startActivity(new Intent(this, LimitActivity.class));
                break;
            case "1000":
                U.toDrainage(this, DrainageActivity.TYPE_REFUSE,Constant.REFUES);
                break;
            case "2000":
                /**
                 * 展示信息
                 */
                mDialog.show();
                mDialog.setMessage(wrapper.getInfo());
                break;
            case "10001":
                showMessage(wrapper.getInfo());
            case "10002":
                /**
                 * 重复发送
                 */
                send();
                mID = wrapper.getCode();
                showMessage(wrapper.getInfo());
                break;
            case Constant.TOKEN_TIMEOUT:
                U.tokenOut(this);
                break;
            case "11000":
            case "10022":
                U.getSingleDialog(this, "提示",
                        "服务密码错误,请重新输入服务密码",
                        "确定", () -> {
                            startActivity(new Intent(this,PhoneActivity.class));
                            finish();
                        }).show();
                break;
            default:
                mDialog.show();
                mDialog.setMessage(wrapper.getInfo());
                break;
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_phoneyzm;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mSubscribe != null && !mSubscribe.isUnsubscribed()){
            mSubscribe.unsubscribe();
        }
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }
}
